package com.ljf.framework.security.token;

import com.ljf.framework.LjfManager;
import com.ljf.framework.exception.LjfTokenException;
import com.ljf.framework.util.LjfStringUtil;

import java.util.UUID;

/**
 * 说明：ljfToken 默认实现
 *
 * @Auther: lijinfeng
 * @Date: 2024/4/11
 */
public class LjfTokenServiceDefaultImpl implements LjfTokenService{

    @Override
    public String createToken(Long userId) {
        return UUID.randomUUID().toString();
    }

    @Override
    public Long getUserIdByToken(String token) {
        String userId = LjfManager.getLjfCacheService().get(LjfTokenConstants.TOKEN_PREFIX + token);
        return Long.parseLong(userId);
    }

    @Override
    public void checkToken(String token) {
        String userId = LjfManager.getLjfCacheService().get(LjfTokenConstants.TOKEN_PREFIX + token);
        if (LjfStringUtil.isEmpty(userId))
            throw new LjfTokenException("token 校验失败，token不存在");
    }
}
